package com.zbkj.crmeb.order.pprocess;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.zbkj.crmeb.chant.request.PurchaseProductRequest;
import com.zbkj.crmeb.merchant.api.request.BuyMemberRequest;
import com.zbkj.crmeb.store.model.StoreOrderInfo;
import com.zbkj.crmeb.user.service.UserMemberRecordService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.constants.Constants;
import com.crmeb.core.exception.CrmebException;
import com.zbkj.crmeb.finance.model.UserRecharge;
import com.zbkj.crmeb.front.request.OrderCreateRequest;
import com.zbkj.crmeb.front.response.OrderPayItemResponse;
import com.zbkj.crmeb.front.response.OrderPayResponse;
import com.zbkj.crmeb.store.model.StoreOrder;
import com.zbkj.crmeb.store.request.StoreOrderRefundRequest;
import com.zbkj.crmeb.store.service.StoreOrderService;
import com.zbkj.crmeb.user.model.User;
import com.zbkj.crmeb.user.request.UserOperateFundsRequest;
import com.zbkj.crmeb.user.service.UserBillService;
import com.zbkj.crmeb.user.service.UserService;

/**
 * @ClassName: yueOrderProcess
 * @Description: 余额支付方式
 */
@Service("yueOrderProcess")
public class YueOrderProcess extends AbstractOrangePayOrderProcess {

    public static final Logger logger = LoggerFactory.getLogger(YueOrderProcess.class);

    @Autowired
    private UserService userService;
    @Autowired
    private StoreOrderService storeOrderService;
    @Autowired
    private UserBillService userBillService;
    @Autowired
    private UserMemberRecordService userMemberRecordService;

    @Override
    public OrderPayResponse creatPayOrder(String key, OrderCreateRequest orderRequest, String ip) {
        StoreOrder storeOrder = super.createOrder(key,orderRequest,ip);

        if(orderRequest.getIsNew()){
            storeOrderService.yuePay(storeOrder, userService.getInfo());
        }
        OrderPayResponse orderPayResponseY = new OrderPayResponse("SUCCESS",
                new OrderPayItemResponse(key, storeOrder.getOrderId()));
        orderPayResponseY.addExtend("orderId",storeOrder.getOrderId());
        return orderPayResponseY;
    }

    @Override
    public HashMap<String, Object> toPayOrder(StoreOrder order, String ip) {
        HashMap<String, Object> resultMap = new HashMap<>();
        boolean b = storeOrderService.yuePay(order, userService.getInfo());
        if(b){
            resultMap.put("result", "SUCCESS");
            resultMap.put("status","yue");
        }else {
            resultMap.put("result", "ERROR");
            resultMap.put("status","yue");
        }
        return resultMap;
    }

    @Override
    public boolean refund(StoreOrderRefundRequest request,StoreOrder storeOrder, StoreOrderInfo storeOrderInfo) {
        UserOperateFundsRequest userOperateFundsRequest = new UserOperateFundsRequest();
        userOperateFundsRequest.setUid(storeOrder.getUid());
        userOperateFundsRequest.setValue(request.getAmount());
        userOperateFundsRequest.setFoundsCategory(Constants.USER_BILL_CATEGORY_MONEY);
        userOperateFundsRequest.setFoundsType(Constants.ORDER_STATUS_REFUNDED);
        userOperateFundsRequest.setType(1);
        userOperateFundsRequest.setTitle(Constants.ORDER_STATUS_STR_REFUNDED);
        boolean addMoney = userService.updateFounds(userOperateFundsRequest, false); //更新余额
        if(!addMoney){throw new CrmebException("余额退款失败");}

        //新增日志
        //用户
        User user = userService.getById(storeOrder.getUid());
        boolean addBill = userBillService.saveRefundBill(request, user);
        if(!addBill){throw new CrmebException("余额退款失败");}
        return goOnRefund(request,storeOrder,storeOrderInfo);
    }

    @Override
    public Object buyMember(BuyMemberRequest request) {
        boolean b = userMemberRecordService.yuePayMember(request);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("result",b ? "SUCCESS" : "ERROR");
        resultMap.put("status","yue");
        resultMap.put("type", request.getFromType());
        return resultMap;
    }

    @Override
    public Map<String, Object> purchaseProduct(PurchaseProductRequest purchaseProductRequest, String ip) {
        return null;
    }

    @Override
    public UserRecharge createImgRecharge(BigDecimal price) {
        // TODO Auto-generated method stub
        return null;
    }

}
